package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Notice;
import com.okq.util.TimeTool;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 公告通知Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 11:36
 */
@Repository
public class NoticeDao extends BaseDao {
    public void saveNotice(Notice notice) {
        super.save(notice);
    }

    public void deleteNotice(Notice notice) {
        super.delete(notice);
    }

    public void updateNotice(Notice notice) {
        super.update(notice);
    }

    public void saveOrUpdateNotice(Notice notice) {
        super.saveOrUpdate(notice);
    }

    public Notice getNoticeById(String id) {
        return (Notice) super.get(Notice.class, id);
    }

    public List<Notice> listAllNotice() {
        String hql = "FROM Notice WHERE isDelete = 0 ";
        return super.find(hql);
    }

    public Page<Notice> pageByNotice(Page<Notice> page, Notice notice) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Notice n WHERE n.isDelete = 0 ");
        if (notice != null) {
            Map<String, Object> param = new HashMap<>();
            String title = notice.getTitle();
            if (StringUtils.isNotBlank(title)) {
                hqlSb.append(" AND n.title LIKE ?1 ");
                param.put("1", "%" + title + "%");
            }
            Integer top = notice.getTop();
            if (top != null && top != -1) {
                hqlSb.append(" AND n.top = ?2 ");
                param.put("2", top);
            }
            String startTime = notice.getStartTime();
            if (StringUtils.isNotBlank(startTime)) {
                hqlSb.append(" AND n.releaseTime >= ?3 ");
                param.put("3", startTime);
            }
            String endTime = notice.getEndTime();
            if (StringUtils.isNotBlank(endTime)) {
                hqlSb.append(" AND n.releaseTime <= ?4 ");
                param.put("4", endTime);
            }
            String comCode = notice.getComCode();
            if (StringUtils.isNotBlank(comCode)) {
                hqlSb.append(" AND n.comCode = ?5 ");
                param.put("5", comCode);
            }
            hqlSb.append(" ORDER BY n.top DESC , n.releaseTime DESC , n.overdueTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    /**
     * 获取最新置顶的已发布的公告通知
     *
     * @return notice
     */
    public Notice getLastTop(String comCode) {
        String nowTime = TimeTool.getNowTime();
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Notice WHERE isDelete = 0 AND top =1 AND isRelease = 1");
        hqlSb.append(" AND overdueTime > '").append(nowTime).append("' ");
        if (StringUtils.isNotBlank(comCode)) {
            hqlSb.append(" AND comCode = '").append(comCode).append("' ");
            hqlSb.append(" ORDER BY releaseTime DESC ");
            return (Notice) super.get(hqlSb.toString());
        }
        return null;
    }

    /**
     * 查询全部未逾期的公告通知
     *
     * @return list
     */
    public List<Notice> listNoOverdueNotice() {
        String hql = "FROM Notice WHERE isDelete = 0 AND overdue = 0";
        return super.find(hql);
    }
}
